package HashPackage2;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Stack;

/**
 * @author Lzm
 * @version 1.0
 */
public class nextGreaterElement_ {


  public int[] nextGreaterElement(int[] nums1, int[] nums2) {
    HashMap<Integer,Integer> map = new HashMap<>();
    int[] result = new int[nums1.length];
    Arrays.fill(result,-1);
    Stack<Integer> stack = new Stack<>();
    for (int i = 0; i < nums1.length; i++) {
      map.put(nums1[i],i);
    }
    stack.push(0);
    for (int i = 1 ; i < nums2.length ; i++){
      if (map.containsKey(nums2[i])){
        while(!stack.isEmpty() && nums2[i] > nums2[stack.peek()]){
          result[map.get(nums2[i])] = nums2[i];
        }
      }
    }
    return result;
  }
}
